package by2019;

import java.util.HashSet;
import java.util.Set;

public class 有效的数独 {

	public static void main(String[] args) {
		
		char[][] c = new char[][] {
			  {'5','3','.','.','7','.','.','.','.'},
			  
			  {'6','.','.','1','9','7','.','.','.'},
			  
			  {'.','9','8','.','.','.','.','6','.'},
			  
			  {'8','.','.','.','6','.','.','.','3'},
			  
			  {'4','.','.','8','.','3','.','.','1'},
			  
			  {'7','.','.','.','2','.','.','.','6'},
			  
			  {'.','6','.','.','.','.','2','8','.'},
			  
			  {'.','.','.','4','1','9','.','.','5'},
			  
			  {'.','.','.','.','8','.','.','7','9'}
		};
		
		System.out.println(isValidSudoku(c));

	}

	public static boolean isValidSudoku(char[][] board) {

		for (int i = 0; i < 9; i++) {
			int h = 0;
			int l = 0;
			Set<Character> hSet = new HashSet<Character>();
			Set<Character> lSet = new HashSet<Character>();

			for (int j = 0; j < 9; j++) {

				char hc = board[i][j];
				char lc = board[j][i];

				if (!(hc == '.')) {
					hSet.add(hc);
					h++;
				}

				if (lc != '.') {
					lSet.add(lc);
					l++;
				}

			}
			
			if(hSet.size() != h || lSet.size() != l)
				return false;
			
		}
		
		int r = 0;
		int w = 0;
		
		for(int i = 0; i < 3; i++) {
			for(int j = 0; j < 3; j++) {
				
				int count = 0;
				Set<Character> set = new HashSet<Character>();
				
				for(int m = 0; m < 3; m++) {
					for(int n = 0; n < 3; n++) {
						if(board[m][n] != '.') {
							set.add(board[r][w]);
							count++;
						}
					}
				}
				
				if(set.size() != count)
					return false;
				
				w=+3;
			}
			r=+3;
		}
		
		return true;

	}

}
